package com.platform.hjt.sys.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.platform.hjt.base.controller.BaseController;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.sys.model.SysCompanyInfo;
import com.platform.hjt.sys.model.SysCompanyModel;
import com.platform.hjt.sys.service.SysCompanyInfoService;
import com.platform.hjt.sys.service.SysModelService;

@Controller
@RequestMapping(value="/sysCompanyInfo")
public class SysCompanyInfoController extends BaseController{

	@Resource
	private SysCompanyInfoService sysCompanyInfoService;
	@Resource
	private SysModelService sysModelService;
	
	@RequestMapping(value="/sysCompanyInfo_list.html")
	public String sysCompanyInfoListPage(){
		return "page/admin/sysCompanyInfo/sysCompanyInfo_list";
	}
	
	@RequestMapping(value="/sysCompanyInfo_list.do")
	@ResponseBody
	public Page<SysCompanyInfo> sysCompanyInfoListDo(Page<SysCompanyInfo>page){
		List<PropertyFilter>  propertyFilterList = PropertyFilter.buildFromHttpRequest(getRequest());
		propertyFilterList.add(new PropertyFilter("EQB_isDel", "false"));
		return sysCompanyInfoService.getSysCompanyInfoList(page, propertyFilterList);
	}
	
	@RequestMapping(value="/sysCompanyInfo_edit.html")
	public String sysCompanyInfoEditPage(HttpServletRequest request, Model model, String id){
		
		if(StringUtils.isNotBlank(id)){
			SysCompanyInfo sysCompanyInfo = sysCompanyInfoService.getSysCompanyInfo("sysCompanyInfoId", id);
			
			List<SysCompanyModel> companyModelList =  sysModelService.getCompanyModelList(id);
			String sysModelIds = "";
			for(SysCompanyModel scm : companyModelList) {
				sysModelIds += scm.getSysModel().getSysModelId() + ",";
			}
			if(StringUtils.endsWith(sysModelIds, ",")){
				sysModelIds = StringUtils.substringBeforeLast(sysModelIds, ",");
			}
			
			sysCompanyInfo.setSysModelIds(sysModelIds);
			model.addAttribute("sysCompanyInfo", sysCompanyInfo);
		}
		return "page/admin/sysCompanyInfo/sysCompanyInfo_edit";
	}
	
	@RequestMapping(value="/sysCompanyInfo_detail_{id}.html")
	public String sysCompanyInfoDetailPage(Model model, @PathVariable String id){
		model.addAttribute("sysCompanyInfo", sysCompanyInfoService.getSysCompanyInfo("sysCompanyInfoId", id));
		return "page/admin/sysCompanyInfo/sysCompanyInfo_detail";
	}
	
	@RequestMapping(value="/sysCompanyInfo_edit.do")
	@ResponseBody
	public ReturnBody sysCompanyInfoEditDo(@Valid @ModelAttribute("sysCompanyInfo") SysCompanyInfo sysCompanyInfo, BindingResult result){
		if(result.hasErrors()){
			return new ReturnBody(false, result.getAllErrors().get(0).getDefaultMessage());
		}
		return sysCompanyInfoService.saveSysCompanyInfo(sysCompanyInfo);
	}
	
	@RequestMapping(value="/sysCompanyInfo_del_{id}.do")
	@ResponseBody
	public ReturnBody sysCompanyInfoDelDo(@PathVariable String id){
		return sysCompanyInfoService.delSysCompanyInfo(new SysCompanyInfo(id));
	}
	
	
}
